
package cn.com.ddapp.ddm.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.bean.DetailsBean;
import cn.com.ddapp.ddm.bean.HomeBean;
import cn.com.ddapp.ddm.util.TDevice;

/**
 * Created by Administrator on 2016/8/18 0018.
 */
public class GoodBrowAdapter extends RecyclerView.Adapter<GoodBrowAdapter.MyViewHolder> {
    private Context context;
    private List<DetailsBean.BrowseGood> guessListBeen;
    private LayoutInflater inflater;

    public GoodBrowAdapter(Context context, List<DetailsBean.BrowseGood> guessListBeen) {
        this.context = context;
        this.guessListBeen = guessListBeen;
        inflater = LayoutInflater.from(context);
    }


    public int getItemCount() {
        return guessListBeen!=null ?guessListBeen.size():0;
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.goodbro, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        holder.classify_namea.setText(guessListBeen.get(position).getName());

        holder.classify_pricea.setText("￥ "+String.valueOf(guessListBeen.get(position).getPrice())+"元");
//        holder.classify_preprice.setText("￥ "+String.valueOf(guessListBeen.get(position).getPriceMk()));
//        holder.classify_preprice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);//加横线
//        holder.classify_sold.setText("销量  "+String.valueOf(guessListBeen.get(position).getSales())+" 件");
//        int width=(int)(TDevice.getScreenWidth())/6;
//        LinearLayout.LayoutParams pa=new LinearLayout.LayoutParams(width,width);
//        holder.classify_img.setLayoutParams(pa);
        String []nUrl=guessListBeen.get(position).getHeadImg().split("\\.");
        StringBuffer buffer=new StringBuffer();
//        if(nUrl[1].equals("jpg")){
//            buffer.append(nUrl[0]).append("_360.jpg");
//        }else if(nUrl[1].equals("png")){
//            buffer.append(nUrl[0]).append("_360.png");
//        }else if(nUrl[1].equals("jpeg")){
//            buffer.append(nUrl[0]).append("_360.jpeg");
//        }
        buffer.append(nUrl[0]).append("_360.").append(nUrl[1]);
        Glide.with(context).load(OKUtilAPI.imgBaseUrl + buffer).into(holder.classify_imga);
        holder.llRoot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(v, position);
                }
            }
        });

        holder.llRoot.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemLongClick(v, position);
                }
                return true;
            }
        });

    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    OnItemClickListener onItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(View v, int position);

        void onItemLongClick(View v, int position);
    }


    class MyViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout llRoot;

        @BindView(R.id.classify_imga)
        ImageView classify_imga;
        @BindView(R.id.classify_namea)
        TextView classify_namea;
        @BindView(R.id.classify_pricea)
        TextView classify_pricea;


        MyViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            llRoot = (LinearLayout) view.findViewById(R.id.llRoot);
        }
    }
}

